package cn.v5;

import java.io.*;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
    private static final String HOST="172.20.12.48";
    private static final Integer PORT=0504;
    private Socket socket;
    private String ip;
    private PrintWriter pw;
    private BufferedReader br;
    private String serverMessage;

    public Client(){
        try {
            socket=new Socket(HOST,PORT);
            ip=socket.getInetAddress().getHostAddress();
            br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF8"));
            pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        } catch (SocketException e){
            System.out.println("客户端链接被重置");
        }catch (UnknownHostException e){
            System.out.println("IP地址有问题，请询问客户端！");
        } catch (IOException e) {
            System.out.println("客户端主动退出了：断开链接");
        }

    }

    private void chat(){
        Scanner input = new Scanner(System.in);
        new Thread(()->{
            System.out.println("客户端请输入：");
            try {
                String message;
                while((message= br.readLine())!=null){//读入服务器端
                    System.out.println("服务器端说："+message);
                    if (message.equalsIgnoreCase("exit")) {
                        System.exit(0);
                        System.out.println("退出聊天~拜拜~");
                    }
                }

            } catch (IOException e) {
                System.out.println("服务器端发来的消息断开了~~");
            }
        }).start();

        while (true) {  //死循环--一直与服务器说话
            String say = input.nextLine();
            pw.println(ip+"客户端说："+say);
            System.out.println("请输入聊天内容(输入exit退出):");
            if (say.equalsIgnoreCase("exit")){
                System.out.println("退出啦~~");
                System.exit(0);
            }
        }
    }

    public void start(){
        System.out.println("~~~~欢迎使用QQ牌聊天室~~~~");
        Scanner input = new Scanner(System.in);
        try {
            serverMessage = br.readLine();
            System.out.println(serverMessage);
            String username=input.nextLine();
            pw.println(username);

            serverMessage= br.readLine();
            System.out.println(serverMessage);
            String password=input.nextLine();
            pw.println(password);

            //三、服务器端验证账号跟密码是否正确
            //3.1读入账号和密码正确的情况
            serverMessage=br.readLine();//登录成功，欢迎进入XX牌聊天室
            System.out.println(serverMessage);//客户端打印：登录成功...
            //3.2 判断如果是“登录成功，欢迎进入XX牌聊天室” 前缀：登录成功
            if(serverMessage.startsWith("登录成功")){ // true 聊天
                chat();  //聊天功能
            }
        } catch (IOException e){
            System.out.println("客户端读写出问题！");
        }finally {
            if (pw!=null)
                pw.close();
            if (br!=null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("服务器通讯内容失败");
                }
            if(socket!=null&&!socket.isClosed()){
                System.out.println("客户端关闭成功！success");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            }
        }
    }


    public static void main(String[] args) {
        new Client().start();
    }
}


